package com.suixun.queue;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.suixun.dao.sendmessagemanage.SendMessageManageDAO;
import com.suixun.entity.SendMessageInfo;
import com.suixun.entity.page.PageModel;
import com.suixun.lib.httpsqs4j.HttpsqsClient;
import com.suixun.util.DateUtil;

public class ScanSendMessageDeal extends BaseDeal{
	protected static Logger logger = LoggerFactory.getLogger(ScanSendMessageDeal.class);
	@Resource
	private SendMessageManageDAO sendMessageManageDAO;
	protected static String queueName = "sendmessage";
	
	@Resource
    private HttpsqsClient syssqs;
	/**
	 * 扫描send_message表，将符合条件的数据放到队列里面
	 * @throws InterruptedException
	 */
	public void execute() throws InterruptedException {
		try{
			PageModel<SendMessageInfo> pageModel=new PageModel<SendMessageInfo>();
			pageModel.getQueryparam().put("isNotPage", "1");
			String date = DateUtil.getTime(new Date(), DateUtil.MINUTE, 2);
			pageModel.getQueryparam().put("date", date);
			List<SendMessageInfo> sendMessages=sendMessageManageDAO.queryByPaging(pageModel);
			if(sendMessages.size()>0){
				for(SendMessageInfo sendMessage:sendMessages){
					int flag = 0;
					if (sendMessage.getSendtime().compareTo(new Date()) == -1) {
						 if(DateUtil.compareToTime2(sendMessage.getSendtime(),new Date()) == 3){
							 flag = 1;
						 }
					}else if (DateUtil.compareTotime3(sendMessage.getSendtime(),new Date()) == 3) {
						flag = 1;
					}
					if (syssqs != null) {
						if (flag == 1) {
							ObjectMapper mapper = new ObjectMapper();
							mapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
							String jsonStr = mapper.writeValueAsString(sendMessage);
							logger.info(jsonStr);
							syssqs.putString(queueName, jsonStr);
						}
					}else {
						logger.error("httpsqsClient is closed");	
					}
				}
			}
		}catch (Exception e) {
			logger.error(e.getMessage(),e);	
		}
	}
}
